package org.jeecg.modules.charge.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.entity.charge.ChargeBills;
import org.jeecg.modules.charge.service.impl.ChargeComMonthReportServiceImpl;
import org.jeecg.vo.charge.ChargeBillsExcel;
import org.jeecg.vo.charge.ComMonthReportReq;
import org.jeecg.vo.charge.ComMonthReportResp;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Description: 综合月报表
* @Author: jeecg-boot
* @Date:   2019-08-08
* @Version: V1.0
*/
@Slf4j
@Api(tags="综合月报表")
@RestController
@RequestMapping("/b/charge/comMonthPort")
public class ChargeComMonthReportController {

    @Autowired
    private ChargeComMonthReportServiceImpl chargeComMonthReportServiceImpl;

    /**
     * 综合月报表
     * @param
     * @return
     */
    @AutoLog(value = "综合月报表")
    @ApiOperation(value="综合月报表", notes="综合月报表")
    @PostMapping(value = "/list")
    public Result<List<ComMonthReportResp>> list(@RequestBody ComMonthReportReq reportReq) {
        log.info("综合月报表查询参数:{}",reportReq);
        try {
            List<ComMonthReportResp>  invoices = chargeComMonthReportServiceImpl.getSumCollectList(reportReq);
            if(invoices.size() <= 0) return Result.ok(invoices);
            Double ext2= 0.0,ext3= 0.0,ext4= 0.0,ext5= 0.0,ext6= 0.0,ext7= 0.0,ext8= 0.0,ext9= 0.0;
            Double ext10= 0.0,ext11= 0.0, ext12= 0.0, ext13= 0.0, ext14= 0.0, ext15= 0.0, ext16= 0.0, ext17= 0.0;
            Double ext18= 0.0, ext19= 0.0, ext20= 0.0, ext21= 0.0, ext22= 0.0, ext23= 0.0, ext24= 0.0;
            for(ComMonthReportResp comMonth :invoices){
                Double sum7 = comMonth.getRealYear()+comMonth.getOwnReal();
                comMonth.setCollecTing(getBigDecimal(sum7)); //收欠费
                Double sum6 =comMonth.getSumShouldYear() + comMonth.getSumOwnYear();
                comMonth.setSumShould(getBigDecimal(sum6)); //合计应收
                Double sum5 = comMonth.getRealMonth() + comMonth.getRealYear() + comMonth.getOwnReal() + comMonth.getAdvanceMonth();
                comMonth.setSumNetReceivde(getBigDecimal(sum5)); //实收合计
                Double sum4 = comMonth.getShouldMonth() - comMonth.getRealMonth() - comMonth.getAdvanceMonthTo();
                comMonth.setHarvestMonth(getBigDecimal(sum4)); // 当月欠收
                Double sum3 = comMonth.getSumShouldYear()-comMonth.getSumrealYear();
                comMonth.setHarvestYear(getBigDecimal(sum3));//当年欠收
                Double sum2 = comMonth.getSumOwnYear()-comMonth.getSumRealOwnYear();
                comMonth.setHarvest(getBigDecimal(sum2));//本期旧欠
                Double sum1 = (comMonth.getSumShouldYear()-comMonth.getSumrealYear())+(comMonth.getSumOwnYear()-comMonth.getSumRealOwnYear());
                comMonth.setSumHarvest(getBigDecimal(sum1));//欠收合计
                comMonth.setReclaimMonth(0.0 + "%");//当月
                if(comMonth.getRealMonth()+comMonth.getAdvanceMonthTo() > 0 && comMonth.getShouldMonth() > 0){
                    Double month = (comMonth.getRealMonth()+comMonth.getAdvanceMonthTo())/comMonth.getShouldMonth() * 100;
                    comMonth.setReclaimMonth(getBigDecimal(month)+"%");//当月
                }
                comMonth.setReclaimYear(0.0+"%");//当年
                if(comMonth.getSumrealYear() > 0 && comMonth.getSumShouldYear() > 0){
                    Double year = comMonth.getSumrealYear()/comMonth.getSumShouldYear() * 100 ;
                    comMonth.setReclaimYear(getBigDecimal(year)+"%");//当年
                }
                comMonth.setReclaimOwn(0.0+"%");//旧欠
                if(comMonth.getSumRealOwnYear() > 0 && comMonth.getSumOwnYear() > 0){
                    Double own =comMonth.getSumRealOwnYear()/comMonth.getSumOwnYear() * 100;
                    comMonth.setReclaimOwn(getBigDecimal(own)+"%");//旧欠
                }
                comMonth.setSumReclaim(0.0+"%");//累计
                if(comMonth.getSumrealYear()+comMonth.getSumRealOwnYear() > 0 && comMonth.getSumShould() > 0 ){
                    Double sum = (comMonth.getSumrealYear()+comMonth.getSumRealOwnYear())/comMonth.getSumShould() * 100;
                    comMonth.setSumReclaim(getBigDecimal(sum)+"%");//累计
                }
              ext2 += comMonth.getShouldMonth();
              ext3 += comMonth.getSumShouldYear();
              ext4 += comMonth.getSumOwnYear();
              ext5 += comMonth.getSumShould();
              ext6 += comMonth.getRealMonth();
              ext7 += comMonth.getRealYear();
              ext8 += comMonth.getOwnReal();
              ext9 += comMonth.getAdvanceMonth();
              ext10 += comMonth.getSumNetReceivde();
              ext11 += comMonth.getAdvanceMonthTo();
              ext12 += comMonth.getAdvanceBlance();
              ext13 += comMonth.getSumrealYear();
              ext14 += comMonth.getSumRealOwnYear();
              ext19 += comMonth.getHarvestMonth();
              ext20 += comMonth.getHarvestYear();
              ext21 += comMonth.getHarvest();
              ext22 += comMonth.getSumHarvest();
              ext23 += comMonth.getCollecTing();
              ext24 += comMonth.getSumAllMoney();
            }
            if(ext6+ext11 > 0 && ext2 > 0){
                ext15 = (ext6+ext11)/ext2 * 100;
            }
            if(ext13 > 0 && ext3 > 0){
                ext16 = ext13/ext3 * 100;
            }
            if(ext14 > 0 && ext4 > 0){
                ext17 = ext14/ext4 * 100;
            }
            if(ext13+ext14 > 0 && ext5 > 0){
                ext18 = (ext13+ext14)/ext5 * 100;
            }
            ComMonthReportResp report = new ComMonthReportResp();
            report.setAddress("合计：");
            report.setBaseKey("sumKey");
            report.setProjectId(reportReq.getProjectId());
            report.setFloorId(reportReq.getFloorId());
            report.setRoomIds(reportReq.getRoomIds());
            report.setShouldMonth(getBigDecimal(ext2));
            report.setSumShouldYear(getBigDecimal(ext3));
            report.setSumOwnYear(getBigDecimal(ext4));
            report.setSumShould(getBigDecimal(ext5));
            report.setRealMonth(getBigDecimal(ext6));
            report.setRealYear(getBigDecimal(ext7));
            report.setOwnReal(getBigDecimal(ext8));
            report.setAdvanceMonth(getBigDecimal(ext9));
            report.setSumNetReceivde(getBigDecimal(ext10));
            report.setAdvanceMonthTo(getBigDecimal(ext11));
            report.setAdvanceBlance(getBigDecimal(ext12));
            report.setSumrealYear(getBigDecimal(ext13));
            report.setSumRealOwnYear(getBigDecimal(ext14));
            report.setReclaimMonth(getBigDecimal(ext15)+"%");
            report.setReclaimYear(getBigDecimal(ext16)+"%");
            report.setReclaimOwn(getBigDecimal(ext17)+"%");
            report.setSumReclaim(getBigDecimal(ext18)+"%");
            report.setHarvestMonth(getBigDecimal(ext19));
            report.setHarvestYear(getBigDecimal(ext20));
            report.setHarvest(getBigDecimal(ext21));
            report.setSumHarvest(getBigDecimal(ext22));
            report.setCollecTing(getBigDecimal(ext23));
            report.setSumAllMoney(getBigDecimal(ext24));
            invoices.add(report);
            return Result.ok(invoices);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return Result.error("系统繁忙!" + e.getMessage());
        }
    }

    /** double计算保留两位小数*/
    private Double getBigDecimal(Double sum){
        BigDecimal bigSum = new BigDecimal(sum);
        Double sums = bigSum.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return sums;
    }

    /**
     * 月报表详情列表
     * @return
     */
    @AutoLog(value = "月报表详情列表")
    @ApiOperation(value="月报表详情列表", notes="月报表详情列表")
    @PostMapping(value = "/detailList")
    public Result<IPage<ChargeBills>> detailList(@RequestBody ComMonthReportReq comMonthReport) {
        try {
            if (comMonthReport.getPageNo() == null) comMonthReport.setPageNo(1);
            if (comMonthReport.getPageSize() == null) comMonthReport.setPageSize(10);
            Page<ChargeBills> page = new Page<ChargeBills>(comMonthReport.getPageNo(), comMonthReport.getPageSize());
            List<ChargeBills> list = chargeComMonthReportServiceImpl.detailList(comMonthReport,page);
            page.setRecords(list);
            return Result.ok(page);
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            return Result.error("系统繁忙!");
        }
    }

    /**
     * 明细导出excel
     *
     * @param response
     */
    @PostMapping(value = "/detailExportXls")
    public ModelAndView detailExportXls(@RequestBody ComMonthReportReq comMonthReport, HttpServletResponse response) {
        log.info("comMonthReport:{}",comMonthReport);
        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<ChargeBills> pageList =  chargeComMonthReportServiceImpl.detailList(comMonthReport,null);
        ChargeBillsExcel collectExcel = new ChargeBillsExcel();
        collectExcel.setAddress("合计");
        collectExcel.setDue(0.00d);
        List<ChargeBillsExcel> billsExcelList = pageList.stream().map(bills->{
            ChargeBillsExcel excel = new ChargeBillsExcel();
            BeanUtils.copyProperties(bills,excel);
            excel.setDue(bills.getDue().doubleValue());
            if (excel.getDue() != null && excel.getDue() > 0) {
                collectExcel.setDue(collectExcel.getDue() + excel.getDue());
            }
            return excel;
        }).collect(Collectors.toList());
        billsExcelList.add(collectExcel);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "账单列表");
        mv.addObject(NormalExcelConstants.CLASS, ChargeBillsExcel.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("账单列表数据","导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, billsExcelList);
        return mv;
    }
}
